package com.portal.framework.util;

import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public final class Debug {

    public static final String noModuleModule = "NoModule";  // set to null for previous behavior

    public static final String SYS_DEBUG = System.getProperty("DEBUG");

    protected static Logger root = Logger.getRootLogger();
    protected static Properties properties = new Properties();
    static {
        try {
        	properties.load(UtilURL.fromResource("log4j.properties").openStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		PropertyConfigurator.configure(properties);
    }

    public static Logger getLogger(String module) {
        if (module != null && module.length() > 0) {
            return Logger.getLogger(module);
        } else {
            return root;
        }
    }
    
    public static void log(Level level, Throwable t, String msg, String module) {
        log(level, t, msg, module, "com.spmanage.framework.util.Debug");
    }

    public static void log(Level level, Throwable t, String msg, String module, String callingClass) {
        Logger logger = getLogger(module);
        if (SYS_DEBUG != null) {
            logger.setLevel(Level.DEBUG);
        }
        logger.log(callingClass, level, msg, t);
        
    }
    
    // leaving these here
    public static void log(String msg) {
        log(Level.INFO, null, msg, noModuleModule);
    }    
    public static void log(Throwable t) {
        log(Level.INFO, t, null, noModuleModule);
    }

    public static void log(String msg, String module) {
        log(Level.INFO, null, msg, module);
    }
    public static void log(Throwable t, String module) {
        log(Level.INFO, t, null, module);
    }

    public static void log(Throwable t, String msg, String module) {
        log(Level.INFO, t, msg, module);
    }


    public static boolean infoOn() {
        return root.isInfoEnabled();
    }
    
    public static boolean debugOn() {
        return root.isDebugEnabled();
    }
    
    public static void logInfo(String msg, String module) {
        log(Level.INFO, null, msg, module);
    }

    public static void logInfo(Throwable t, String module) {
        log(Level.INFO, t, null, module);
    }

    public static void logInfo(Throwable t, String msg, String module) {
        log(Level.INFO, t, msg, module);
    }
    public static void logDebug(String msg, String module) {
        log(Level.DEBUG, null, msg, module);
    }

    public static void logDebug(Throwable t, String module) {
        log(Level.DEBUG, t, null, module);
    }

    public static void logDebug(Throwable t, String msg, String module) {
        log(Level.DEBUG, t, msg, module);
    }
    public static boolean warningOn() {
        return true;
    }

    public static void logWarning(String msg, String module) {
        log(Level.WARN, null, msg, module);
    }

    public static void logWarning(Throwable t, String module) {
        log(Level.WARN, t, null, module);
    }

    public static void logWarning(Throwable t, String msg, String module) {
        log(Level.WARN, t, msg, module);
    }

    public static boolean errorOn() {
        return true;
    }

    public static void logError(String msg, String module) {
        log(Level.ERROR, null, msg, module);
    }

    public static void logError(Throwable t, String module) {
        log(Level.ERROR, t, null, module);
    }

    public static void logError(Throwable t, String msg, String module) {
        log(Level.ERROR, t, msg, module);
    }

    public static boolean fatalOn() {
        return true;
    }

    public static void logFatal(String msg, String module) {
        log(Level.FATAL, null, msg, module);
    }

    public static void logFatal(Throwable t, String module) {
        log(Level.FATAL, t, null, module);
    }

    public static void logFatal(Throwable t, String msg, String module) {
        log(Level.FATAL, t, msg, module);
    }

}
